<?php 
  // Import PHPMailer classes into the global namespace.  isUsedCode = jiangxde163
  // These must be at the top of your script, not inside a function
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;

  //Load composer's autoloader
  require 'vendor/autoload.php';

  //设置代理邮箱
  $account = 'jiangxde163@163.com';
  $pwd = 'jiangxde163';
  $host = 'smtp.163.com;';

  //输入内容
  $name = $_POST['name'];
  $message = $_POST['message'];
  $phone = $_POST['phone'];
  $email = $_POST['email'];
  $qq = $_POST['qq'];

  //设置信息
  $send_to = ['wiever@foxmail.com', 'isjx@foxmail.com', 'iscrj@foxmail.com'];   //收件人
  $subject = 'gole.site留言';   //主题
  $body = '<div>姓名：'.$name.'</div><div>电话：'.$phone.'</div><div>qq：'.$qq.'</div><div>邮箱：'.$email.'</div><div>需求：'.$message.'</div>';                    //内容

  /**
   * 验证输入内容
   * @param  [type] $name    [姓名]
   * @param  [type] $message [需求]
   * @param  [type] $phone   [电话]
   * @return [type]          [返回内容]
   */
  function sendVerfy($name,$message,$phone)
    {
      $return_array = array();
      $return_array['success'] = '1';
      $return_array['msg'] = '';
      if($name == '')
      {
        $return_array['success'] = '0';
        $return_array['msg'] = '请输入您的姓名';
      }else if($phone == '')
      {
        $return_array['success'] = '0';
        $return_array['msg'] = '请填写正确的电话号码';
      }else if($message == '')
      {
        $return_array['success'] = '0';
        $return_array['msg'] = '请简述您的需求';
      }
      else if (strlen($message) < 2) {
          $return_array['success'] = '0';
          $return_array['msg'] = '请输入不少于2个字符的需求简述';
      }
      return $return_array;
    }

  function sendEmail($account, $pwd, $send_to, $subject, $body, $host){
    $mail = new PHPMailer();                              // Passing `true` enables exceptions
    //Server settings
    $mail->CharSet = "utf-8";
    $mail->SMTPDebug = 1;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = $host;  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = $account;                 // SMTP username
    $mail->Password = $pwd;                           // SMTP password
    $mail->SMTPSecure = 'ssl';                         // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom($account, $subject);
    foreach ($send_to as $key => $value) {
      $mail->addAddress($value, $subject);     // Add a recipient
    }
    $mail->addReplyTo($account, $subject);

    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         // 添加附件
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // 添加图片

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $body;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if($mail->send())
      return 1;
    else
      return 0;
  }

  $return_array = sendVerfy($name, $message, $phone);

  if($return_array['success'] == '1'){
    if (!sendEmail($account, $pwd, $send_to, $subject, $body, $host)){
      $return_array['success'] = '0';
      $return_array['msg'] = '发送失败，请选择其他方式联系我';
    }
  }
  header('Content-type: text/json');
  echo json_encode($return_array);
  die();
?>

